package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.HuiBangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "huibang")
public class HuiBangAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(HuiBangAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String product = orderBean.getProductCode();
		String phone = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();//订购流水号
		String notifyUrl = BetweenProperties.CALLBACK_URL+"Huibang";//回调路径
		
		
		String result = HuiBangServiceImpl.service.order(product,phone,tradeNo,notifyUrl);
	
		
		JSONObject json = JSONObject.fromObject(result);

		String resultCode = json.getString("resultCode");//状态码

		if ("0".equals(String.valueOf(resultCode))) {
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("Msg"));
			orderBean.setErrorMsg(json.getString("Msg"));
			orderBean.setSuppOrderNo(json.getString("orderNo"));
		} else {
			
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("Msg"));
			orderBean.setErrorMsg(json.getString("Msg"));
			
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
		
	}

	
	public void status() {
	
		
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getSuppOrderNo();
		if ("".equals(cstmOrderNo)) {
			orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {
			String result = HuiBangServiceImpl.service.orderStatus(cstmOrderNo);
			log.info("between- 广州汇邦HuiBangService.orderStatus() 查询响应response<> :" + result);
		

			JSONObject json = JSONObject.fromObject(result);

			String resultCode = json.getString("resultCode");

			if ("0".equals(resultCode)) {
				String orderSate = json.getString("orderSate");
				if ("001".equals(orderSate)||"002".equals(orderSate)||"003".equals(orderSate)) {
					orderBean.setStatus("1");
					orderBean.setMessage("订单在受理中");
					orderBean.setErrorMsg("订单在受理中");
				}else if ("009".equals(orderSate)) {
				
					orderBean.setStatus("7");
					orderBean.setMessage("充值成功");
					orderBean.setErrorMsg("充值成功");
				}else {
					orderBean.setStatus("8");
					orderBean.setMessage("充值失败");
					orderBean.setErrorMsg("充值失败");
				}
				
			}else{
				orderBean.setStatus("1");
				orderBean.setMessage("订单在受理中");
				orderBean.setErrorMsg("订单在受理中");
			} 
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
	
	
}
